//
//  AssetPasswordController.m
//  TenMinDemo
//
//  Created by Shawn Li on 2021/3/9.
//  Copyright © 2021 CYXiang. All rights reserved.
//

#import "AssetPasswordController.h"
#import "SafetyConfirmView.h"
#import "BaseTextField.h"
#import "CYXBaseRequest.h"
#import "PicVerCodeView.h"
#import "VerifyPswViewController.h"

@interface AssetPasswordController ()
@property (nonatomic,strong) SafetyConfirmView* safetyView;
@property (weak, nonatomic) IBOutlet BaseTextField *pswInput;
@property (weak, nonatomic) IBOutlet UILabel *hid;
@property (weak, nonatomic) IBOutlet BaseTextField *cPswInput;
@end

@implementation AssetPasswordController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)setupUI{
    [super setupUI];
    self.title = LCLocalString(@"psw_zjmm");
    self.pswInput.placeholder = LCLocalString(@"fpsw_qsrymm");
    self.pswInput.placeholderColor = PlhColor;
    self.cPswInput.placeholder = LCLocalString(@"cfm_zjmm");
    self.cPswInput.placeholderColor = PlhColor;
    
    UserInfoModel *user = kAppDelegate.curUser;
    self.hid.text = user.hid;
}

- (BOOL)chenkInput{
    
    if (!self.pswInput.text.length) {
        LCInfoHUD(self.pswInput.placeholder, 1);
        return  NO;
    }
    if (!self.cPswInput.text.length) {
        LCInfoHUD(self.cPswInput.placeholder, 1);
        return  NO;
    }
    if (![self.pswInput.text isEqualToString:self.cPswInput.text]) {
        LCInfoHUD(LCLocalString(@"lcmmbt"), 1);
        return  NO;
    }
    return YES;
}

- (IBAction)confirmAction:(id)sender {
    if (![self chenkInput]) {
        return;
    }
    LRWeakSelf(self);
    if (!self.safetyView) {
        self.safetyView = [[SafetyConfirmView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREENH_HEIGHT)];
        LRWeakSelf(self)
        self.safetyView.confirmType = @"2";
        self.safetyView.actionBlock = ^(NSInteger actionFlag,NSDictionary *info) {
            [weakself.view endEditing:YES];
            if (actionFlag == 0) {

            }else if (actionFlag == 1){
                
//                [weakself showPrompt:@"23423423423"];
//                [weakself optPsw_net:info];
                [weakself pushLoginPswcheck:info];
            }else if (actionFlag == 2){
                [weakself getPicVerCode_net];
//                [weakself getPicVerCode_net];
            }
        };
    }
    self.safetyView.phoneNum = kAppDelegate.curUser.phone;
    [self.safetyView showInView:self.navigationController.view];
}

- (void)getPvc_net:(NSString *)gvCode{
//    if (![self checkPhone]) return;
    
    NSDictionary *param = @{@"gvc":gvCode};
    
        [CYXBaseRequest postWithUrl:API_sendResetPayPasswordPvc param:param resultClass:nil success:^(id res) {
            if ([res isEqual: RequestErrorCode]) {
                
            }else{
                LCSuccessHUD(@"驗證碼已發送", 1);
                [self.safetyView verBeginRun];
            }
            
            CYXLog(@"%@",res)
        } failure:^(NSError *error) {
            CYXLog(@"%@",error)
        }];
}

- (void)getPicVerCode_net{
    PicVerCodeView *view = [[PicVerCodeView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREENH_HEIGHT)];
    view.gvcApi = API_getGvc;
    LRWeakSelf(self);
    view.actionBlock = ^(NSInteger index,NSString *gvCode) {
        CYXLog(@"%zd",index);
        if(index == 1){
            [weakself.view endEditing:YES];
            [weakself getPvc_net:gvCode];
        }
    };
    
    [view showInView:self.navigationController.view];
}


- (void)pushLoginPswcheck:(NSDictionary *)info{

    NSDictionary *param = @{
        @"code":info[@"verCode"],
        @"password":self.pswInput.text,
        @"loginPassword":info[@"pswCode"],
        
    };
    
    
    [CYXBaseRequest postWithUrl:API_resetPayPasswordCommit param:param resultClass:nil success:^(id res) {
        if ([res isEqual: RequestErrorCode]) {
            
        }else{
//            self.coinAddr = res[@"address"];
//            [self showQRCode];
            LCSuccessHUD(@"修改成功", 1.5);
            [self.navigationController popViewControllerAnimated:YES];
        }
        
        CYXLog(@"%@",res)
    } failure:^(NSError *error) {
        CYXLog(@"%@",error)
    }];
    
//    VerifyPswViewController *vc= LoadVCFormXib(VerifyPswViewController);
//    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark-

-(void)keyboardChangeFrame:(NSNotification*)noti{

    CGRect rect= [noti.userInfo[UIKeyboardFrameEndUserInfoKey]CGRectValue];
    NSLog(@"%@", noti.userInfo);
    CGFloat keyboardheight = rect.size.height;
    CGFloat duration=[noti.userInfo[UIKeyboardAnimationDurationUserInfoKey]doubleValue];
    CGFloat curve=[noti.userInfo[UIKeyboardAnimationCurveUserInfoKey]doubleValue];

    [UIView setAnimationCurve:curve];
    [UIView animateWithDuration:duration animations:^{
        self.safetyView.bgBottom.constant = keyboardheight - 30;
        [self.safetyView layoutIfNeeded];
    } completion:^(BOOL finished) {
        
    }];
}

-(void)keyboardHide:(NSNotification *)noti
{
    [self.view endEditing:YES];

    [UIView animateWithDuration:0.35 animations:^{
        self.safetyView.bgBottom.constant = 0;
        [self.safetyView layoutIfNeeded];
      
    } completion:^(BOOL finished) {
        
    }];

}

@end
